package demo1;

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student> {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //return this.name.compareTo(o.name);
       return this.age - o.age;

    }
}


class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class Test {

    public static void mySort(Comparable[] comparables) {
        for (int i = 0; i < comparables.length - 1; i++) {
            for (int j = 0; j < comparables.length - 1 - i; j++) {
                if (comparables[j].compareTo(comparables[j + 1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }

            }

        }
    }

    public static void main(String[] args) {
        Student student1 = new Student("liming", 17);
        Student student2 = new Student("zhengsan", 27);

        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student1, student2));

        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1, student2));

        System.out.println("===========");

        Student[] students = new Student[4];
        students[0] = new Student("aiming", 17);
        students[2] = new Student("bwe", 89);
        students[1] = new Student("chengsan", 27);
        students[3] = new Student("dhengsan", 2);

        AgeComparator ageComparator1 = new AgeComparator();
        Arrays.sort(students,ageComparator1);
        System.out.println(Arrays.toString(students));

        NameComparator nameComparator1 = new NameComparator();
        Arrays.sort(students,nameComparator1);
        System.out.println(Arrays.toString(students));
    }
    public static void main2(String[] args) {

        Student[] students = new Student[4];
        students[0] = new Student("aiming", 17);
        students[2] = new Student("bwe", 89);
        students[1] = new Student("chengsan", 27);
        students[3] = new Student("dhengsan", 2);

       // Arrays.sort(students);
        mySort(students);
        System.out.println(Arrays.toString(students));

    }
    public static void main1(String[] args) {
       /* Student student1 = new Student("liming", 17);
        Student student2 = new Student("zhengsan", 27);

        if (student1.compareTo(student2) > 0) {
            System.out.println("student1 > student2");
        } else {

            System.out.println("student1 <= student2");
        }
*/

     }
}